function devTable  = EquipmentConnect( connectionFile )
%EquipmentConnect �˴���ʾ�йش˺�����ժҪ
%   �˴���ʾ��ϸ˵��
    if nargin < 1
        connectionFile = [Constant.AutoMeasurementRootPath '\+EquipmentControl\+AuxiliaryEquipment\+Connection\DEFULT_DEVICE.dat'];
    end
    devTable = EquipmentControl.AuxiliaryEquipment.Connection.getConnectionTable(connectionFile);
    
   for k=1:size(devTable,1)
       devTable = connect_device(devTable, k);
   end

end

function devTbl  = connect_device(devTbl, k)
    tblCell = devTbl.Variables;
    name = tblCell{k, 1};
    type = tblCell{k, 2};
    class = tblCell{k, 3};
    conn = tblCell{k, 4};
    id = tblCell{k, 5};
    try 
        opt = tblCell(k, 6);
    catch
        opt = 'none';
    end

    switch conn
        case 'Ethernet'
            visa = 'visa-tcpip';
            idStr = ['TCPIP0::',  id, '::inst0::INSTR'];
        case 'USB'
            visa = 'visa-usb';
            if strcmp(type, 'NIDAQ')
                idStr = id;
            else
                idStr = id;
%                 idStr = ['USB0::',  id, '::inst0::INSTR'];
            end
        case 'COM'
            idStr = id;
        case devTbl.name
            fprintf('\t execution device controlled by %s.\n', conn);
        otherwise
            error('Unknown connection type %s.', conn);
    end
    
    switch type
        case 'AWG'
            cmd = sprintf('EquipmentControl.SignalEquipment.%s.%s(''%s'', ''%s'', ''%s'');\n', type, class, name, visa, idStr);
        case {'NIDAQ', 'SRS'}
            cmd = sprintf('EquipmentControl.%s.%s(''%s'');\n', type, class, idStr);            
        case 'ZI'
            isSync = 'false';
            if strcmp(opt, 'sync')
                isSync = 'true';
            end
            
            cmd = sprintf('EquipmentControl.ZI.%s(''%s'', ''level'', 6, ''multiDeviceSync'', %s);\n', class, name, isSync);            
        case 'LASER'
            cmd = sprintf('EquipmentControl.ExecutionEquipment.ECDL.%s(''%s'');\n', class, name);            
        case 'AOM'
            cmd = sprintf('EquipmentControl.ExecutionEquipment.AOM.%s(''%s'');\n', class, name);
        otherwise
            error('Unknown device type:%s', type);
    end
    
    fprintf('Conneting to device: \t%s', cmd);
    
    try
        dev_k = eval(cmd);
        devTbl{k, 7}={'Connected'};
        devTbl{k, 8}={dev_k};
    catch
        disp(cmd);
        error('Unable to connect to device %s', name);
    end
end
